package com.gillion.grule.client.service.impl;

import com.gillion.grule.client.core.Context;
import com.gillion.grule.client.service.InParameterMustService;
import com.gillion.grule.client.utils.ContextHolder;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
import org.apache.commons.collections.map.HashedMap;
import org.mvel2.MVEL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;




public class RrsInParameterMustServiceImpl implements InParameterMustService
{
    Logger logger = LoggerFactory.getLogger(RrsInParameterMustServiceImpl.class);


    public void exccute(String express, Context context) {
        String[] expres = express.split("\\|");

        String accountSubject = getStringValue("params[3]", context);
        String businessId = getStringValue("params[1].billId", context);
        String accountObjectType = getStringValue("params[1].billType", context);
        String node = getStringValue("params[1].nodeCode", context);
        Integer expectedNodeOrActualNode = getIntegerValue("params[1].nodeType", context);
        String accountObjectId = getStringValue("params[1].dcoId", context);


        try {
            HashedMap hashedMap = new HashedMap();
            hashedMap.put("exceptionInput", "no exceptionInput");
            hashedMap.put("exceptionTime", new Date());
            hashedMap.put("exceptionCode", "060102");
            hashedMap.put("accountSubject", accountSubject);
            hashedMap.put("businessId", businessId);

            hashedMap.put("exceptionOutput", "计费对象清洗字段：" + expres[2] + " 为空");
            hashedMap.put("accountObjectType", accountObjectType);
            hashedMap.put("node", node);
            hashedMap.put("expectedNodeOrActualNode", expectedNodeOrActualNode);
            hashedMap.put("accountObjectId", accountObjectId);
            setMap(accountObjectType, (Map)hashedMap, context);
            Class<?> clazz = Class.forName("com.haier.rrsbms.fou.dto.FouExceptionInfoDTO");
            Class[] classArray = new Class[1];
            classArray[0] = clazz;
            Object client = ContextHolder.getBean("fouExceptionInfoServiceClient");
            Method method = client.getClass().getDeclaredMethod("saveAccountExceptionInfoSelective", classArray);
            Object param = clazz.newInstance();

            BeanUtils.copyProperties(param, hashedMap);
            method.invoke(client, new Object[] { param });
        } catch (Exception e) {
            this.logger.error("模型初始化调用异常服务出错", e);
        }
    }

    public void setMap(String accountObjectType, Map map, Context context) {
        if ("TMS_LEG".equals(accountObjectType)) {
            setTmsLegMap(map, context);
        } else if ("TMS_SHIPMENT".equals(accountObjectType)) {
            setTmsShipment(map, context);
        } else if ("WMS_INOUT".equals(accountObjectType)) {
            setWmsInout(map, context);
        } else if ("OMS_ORDER".equals(accountObjectType)) {
            setOmsOrder(map, context);
        } else if ("S1005".equals(accountObjectType) || "S1072".equals(accountObjectType) || "S0025".equals(accountObjectType) || "S1008"
                .equals(accountObjectType) || "RRS_KOUKUAN".equals(accountObjectType) || "S1019".equals(accountObjectType)) {

            setWellen(map, context);
        } else if ("OMS_JDINOUT".equals(accountObjectType)) {
            setOMSJdInOut(map, context);
        } else if ("BI_PRO_DB_OUT".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datBiProDbOutDTO.baseCode", context));
        } else if ("BI_PRO_DB_TASK".equals(accountObjectType)) {
            map.put("platform", getStringValue("QD10", context));
        } else if ("BI_WMS_INOUT_ACCOUNT".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datCosmoWmsInoutAccountDTO.baseCode", context));
        } else if ("IMP_INOUT".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datOnceInoutDetailDataWashDTO.platformCode", context));
        } else if ("INTELLECT_WH".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].platFormCode", context));
        } else if ("ITMS_DISPATCH".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datItmsDispatchInfoDTO.tplst", context));
        } else if ("ITMS_SECOND_DETAIL".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datTptSecondDetailDTO.baseCode", context));
        } else if ("ITMS_USE_CAR".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datTptUseCarAssignmentDTO.basecode", context));
        } else if ("LES_DAODUANINOUT".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datInOutShortTranDataWashDTO.tplst", context));
        } else if ("LES_JP_LEG".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datBillingReturnStatusDTO.tplst", context));
        } else if ("LES_LEG".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datBillingReturnStatusDTO.tplst", context));
        } else if ("LES_RETURNFACTORY".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datBadGoodsRetFacDataWashDTO.tplst", context));
        } else if ("LES_UNDERWRITING".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datLesUnderwritingDTO.baseCode", context));
        } else if ("ITMS_VEHICLE_ASSIGNMENT".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].baseCode", context));
        } else if ("ITMS_ASSIGNMENT_DETAIL".equals(accountObjectType)) {
            map.put("platform", getStringValue("params[0].datTptAssignmentDetailDTO.baseCode", context));
        }
    }

    public void setTmsLegMap(Map map, Context context) {
        map.put("platform", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.implementPlatFormCode", context));
        map.put("platformName", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.platformName", context));
        map.put("businessNo", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.hawb", context));
        map.put("accountCustomer", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.customerCode", context));
        map.put("accountCustomerName", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.customerName", context));
        map.put("accountProvider", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.carrierCode", context));
        map.put("accountProviderName", getStringValue("params[0].legDispatchInfoDTO.carrierName", context));
        map.put("businessTime", getDateValue("params[0].legDispatchInfoDTO.activeTime", context));
        map.put("exceptionInfo", "-");
        map.put("accountObjectType", "TMS_LEG");
        map.put("accountObjectTypeName", "TMS运单");
        map.put("origin", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.fromName", context));
        map.put("destination", getStringValue("params[0].legWaybillJsonWithDetialTransDTO.toName", context));
        map.put("module", Integer.valueOf(2));
    }

    public void setTmsShipment(Map map, Context context) {
        map.put("platform", getStringValue("params[0].dispatchInfoDTO.platFormCode", context));
        map.put("platformName", getStringValue("params[0].dispatchInfoDTO.carrierName", context));
        map.put("businessNo", getStringValue("params[0].dispatchInfoDTO.code", context));
        map.put("accountCustomer", "-");
        map.put("accountCustomerName", "-");
        map.put("accountProvider", getStringValue("params[0].dispatchInfoDTO.carrierCode", context));
        map.put("accountProviderName", getStringValue("params[0].dispatchInfoDTO.carrierName", context));
        map.put("businessTime", getDateValue("params[0].legDispatchInfoDTO.activeTime", context));
        map.put("exceptionInfo", "-");
        map.put("accountObjectType", "TMS_SHIPMENT");
        map.put("accountObjectTypeName", "TMS调度单");
        map.put("origin", "-");
        map.put("module", Integer.valueOf(2));
    }


    public void setWmsInout(Map map, Context context) {
        String businessNo = getStringValue("params[0].datWmsInOutWarehouseFlow.mblnr", context);
        if ("-".equals(businessNo)) {
            businessNo = getStringValue("params[0].datWmsInOutWarehouseFlow.bstkd", context);
        }
        map.put("platform", getStringValue("params[0].datWmsInOutWarehouseFlow.spCode", context));
        map.put("platformName", getStringValue("params[0].datWmsInOutWarehouseFlow.platformName", context));
        map.put("businessNo", businessNo);
        map.put("accountCustomer", getStringValue("params[0].datWmsInOutWarehouseFlow.customerCode", context));
        map.put("accountCustomerName", "-");
        map.put("accountProvider", getStringValue("params[0].datWmsInOutWarehouseFlow.warehouseDesignatedSettlement", context));
        map.put("accountProviderName", getStringValue("params[0].datWmsInOutWarehouseFlow.warehouseName", context));
        map.put("businessTime", getDateValue("params[0].legDispatchInfoDTO.activeTime", context));
        map.put("exceptionInfo", "-");
        map.put("accountObjectType", "WMS_INOUT");
        map.put("accountObjectTypeName", "WMS出入库明细");
        map.put("origin", "-");
    }


    public void setOmsOrder(Map map, Context context) {
        map.put("module", Integer.valueOf(4));
        map.put("accountObjectType", "OMS_ORDER");
        map.put("accountObjectTypeName", "OMS订单");
        map.put("exceptionInfo", "-");
        map.put("businessNo", getStringValue("params[0].omsOrderNodeInfoDataDTO.orderNo", context));
        map.put("platform", getStringValue("params[0].omsOrderNodeInfoDataDTO.custCode", context));
        map.put("platformName", getStringValue("params[0].omsOrderNodeInfoDataDTO.custName", context));
        map.put("origin", getStringValue("params[0].omsOrderNodeInfoDataDTO.custName", context));
        map.put("destination", getStringValue("params[0].omsOrderNodeInfoDataDTO.storeCode", context));
        map.put("accountSubject", getStringValue("params[3]", context));
        map.put("businessTime", getDateValue("params[0].omsOrderNodeInfoDataDTO.omsOrderNodeDataDTO.nodeTime", context));
        map.put("accountCustomer", getStringValue("params[0].omsOrderNodeInfoDataDTO.customerCode", context));
    }

    public void setWellen(Map map, Context context) {
        map.put("platform", getStringValue("params[0].datVirtualBilling.paltformCode", context));
        map.put("accountCustomer", getStringValue("params[0].datVirtualBilling.customerCode", context));
        map.put("accountProvider", getStringValue("params[0].datVirtualBilling.supplierCode", context));
        map.put("businessTime", getDateValue("params[0].datVirtualBilling.sumEndTime", context));
        map.put("accountObjectType", "TMS_SHIPMENT");
        map.put("accountObjectTypeName", "TMS调度单");
        map.put("origin", getStringValue("params[0].datVirtualBilling.sumObjFlocation", context));
        map.put("destination", getStringValue("params[0].omsOrderNodeInfoDataDTO.orderNo", context));
        map.put("accountSubject", getStringValue("params[3]", context));
        map.put("accountCustomerName", getStringValue("params[0].datVirtualBilling.customerName", context));
    }


    public void setOMSJdInOut(Map map, Context context) {
        map.put("platform", getStringValue("params[0].datJdInoutDetailWashDTO.spCode", context));
        map.put("platformName", "-");
        map.put("businessNo", getStringValue("params[0].datJdInoutDetailWashDTO.mblnr", context));
        map.put("accountCustomer", getStringValue("params[0].datJdInoutDetailWashDTO.customerCode", context));
        map.put("accountCustomerName", "-");
        map.put("accountProvider", getStringValue("params[0].datJdInoutDetailWashDTO.warehouseDesignatedSettlement", context));
        map.put("accountProviderName", "-");
        map.put("businessTime", getDateValue("params[0].datJdInoutDetailWashDTO.acountDate", context));
        map.put("exceptionInfo", "-");
        map.put("accountObjectType", "OMS_JDINOUT");
        map.put("accountObjectTypeName", "京东出入库明细");
    }

    public String getStringValue(String express, Context context) {
        String result;
        try {
            result = MVEL.eval(express, context).toString();
        } catch (Exception e) {
            this.logger.info("异常信息初始化失败,初始化表达式：" + express, e);
            result = "-";
        }
        return result;
    }


    public Integer getIntegerValue(String express, Context context) {
        Integer result;
        try {
            result = Integer.valueOf(Integer.parseInt(MVEL.eval(express, context).toString()));
        } catch (Exception e) {
            this.logger.info("异常信息初始化失败,初始化表达式：" + express, e);
            result = Integer.valueOf(0);
        }
        return result;
    }

    public Object getDateValue(String express, Context context) {
        Object result;
        try {
            result = MVEL.eval(express, context);
        } catch (Exception e) {
            this.logger.info("异常信息初始化失败,初始化表达式：" + express, e);
            result = null;
        }
        return result;
    }
}